package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.exception.ErrorCodeEnum;
import com.kelvem.saas.workbanch.core.exception.SaasException;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 实体控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/mock")
public class MockController {


    /**
     * _throws
     * curl -X POST 'http://localhost:9090/mock/error?code=506&msg=eroor_msg' -H 'Content-Type: application/json' -d "{}"
     *
     * @param code 错误编码
     * @param msg 错误消息
     * @return 任意返回
     */
    @PostMapping("/error")
    public Object mockErorr(@RequestParam(required = false) Integer code, @RequestParam(required = false) String msg, HttpServletResponse response) {

        // 如果发生错误，设置状态码为 506 并返回错误消息
        response.setStatus(code);
        response.setHeader("code", code.toString());
        response.setHeader("msg", msg);
        return "_erorr_erorr";
    }

    /**
     * _throws
     * curl -X POST http://localhost:9090/mock/throw -H 'Content-Type: application/json' -d "{}"
     */
    @PostMapping("/throw")
    public Object mockThrow() {
        throw new SaasException(ErrorCodeEnum.COMMON_PARAM_EMPTY);
    }

    /**
     * _throws
     * curl -X POST http://localhost:9090/mock/test -H 'Content-Type: application/json' -d "{}"
     */
    @PostMapping("/test")
    public Map<String, String> test() {
        Map<String, String> map = new HashMap<>();
        map.put("msg", "test");
        return map;
    }
}

